<script setup>
  import { ref, watch, watchEffect } from 'vue'

  const props = defineProps({
    open: {
      type: Boolean,
      default: false
    }
  })

  const emit = defineEmits(['close'])

  const show = ref(props.open)

  watch(() => props.open, (n) => {
    show.value = n
  })

  function close() {
    show.value = false
    emit('close')
  }
</script>

<template>
  <Teleport to="body">
    <div class="relative z-40 " :class="{'show': show}">
      <Transition name="fade">
        <div class="fixed inset-0 bg-gray-600 bg-opacity-75 opacity-100" v-if="show"></div>
      </Transition>

      <Transition name="slide-left">
        <div class="fixed inset-0 z-40 flex" @click="close" v-if="show">
          <div class="relative flex w-full max-w-xs flex-1 flex-col bg-gray-900 translate-x-0" @click.stop>
            <div class="absolute top-0 right-0 -mr-12 pt-2 opacity-100">
              <button type="button" class="ml-1 flex h-10 w-10 items-center justify-center focus:outline-none focus:ring-2 focus:ring-inset focus:ring-white" tabindex="0"
                @click="close">
                <span class="sr-only">Close sidebar</span>
                <svg stroke="currentColor" fill="none" stroke-width="1.5" viewBox="0 0 24 24" stroke-linecap="round" stroke-linejoin="round" class="h-6 w-6 text-white" height="1em"
                  width="1em" xmlns="http://www.w3.org/2000/svg">
                  <line x1="18" y1="6" x2="6" y2="18"></line>
                  <line x1="6" y1="6" x2="18" y2="18"></line>
                </svg>
              </button>
            </div>

            <slot></slot>
          </div>
          <div class="w-14 flex-shrink-0"></div>
        </div>
      </Transition>
    </div>
  </Teleport>
</template>


<style scoped>
  .fade-enter-active,
  .fade-leave-active {
    transition: opacity .2s ease;
  }

  .fade-enter-from,
  .fade-leave-to {
    opacity: 0;
  }

  .slide-left-enter-active,
  .slide-left-leave-active {
    transition: transform .2s ease;
  }

  .slide-left-enter-from,
  .slide-left-leave-to {
    transform: translateX(-100%);
  }
</style>